<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Grid实现瀑布流</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        /* grid-template-rows: 1fr 1fr 1fr; */
        column-gap: 5px;
    
        /* grid-row-gap: 10px; */
        /* height: 540px; */
        grid-auto-rows: 10px;
        /* 设置多余的网格高度 */
      }
      .item {
        width: 200px;
        /* margin-bottom: 10px; */
      }
      .item1 {
        /* grid-row-start: 1; */
        /* grid-row-end: span 2; */

        /* grid-column-start: 2; */
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
      <div class="item">6</div>
      <div class="item">7</div>
      <div class="item">8</div>
      <div class="item">9</div>
    </div>

    <script>
      let heights = [150, 160, 170, 180, 140, 155, 165, 180, 175]
      let colors = [
        '#ef3429',
        '#f68f25',
        '#4ba846',
        '#0476c2',
        '#c077af',
        '#f8d29d',
        '#b4a87f',
        '#d0e4a8',
        '#4dc7ec'
      ]
      let items = document.getElementsByClassName('item')

      console.log(items.length);
      for (let i = 0; i <= items.length; i++) {
        // items[i].style.height = '100%'
      
        items[i].style.gridRowEnd = 'span ' + Math.round(heights[i] / 10)
        // console.log(items[i].style.gridRowEnd)
        // items[i].style.height = heights[i] + 'px'
        items[i].style.background = colors[i]
      }
    </script>
  </body>
</html>
